//
//  GYDetailsHeadView.m
//  Visitor
//
//  Created by x on 2017/9/14.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYDetailsHeadView.h"
#import "UILabel+GYLable.h"
#import "UIImageView+GYExtension.h"

@interface GYDetailsHeadView ()

/** 访客姓名 */
@property (nonatomic, weak) UILabel *visitorLable;

/** 访客时间 */
@property (nonatomic, weak) UILabel *timeLable;

/** 身份证图片 */
@property (nonatomic, weak) UIImageView *IDImageView;

/** 现场拍照图片 */
@property (nonatomic, weak) UIImageView *photoImageView;

/** 该条访客记录的状态 */
@property (nonatomic, weak) UILabel *stateLable;

@end
@implementation GYDetailsHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.初始化
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    //1.添加子控件
    UILabel *visitorLable = [UILabel lableWithMark:@"陈胜" titleColor:RGBColor(0X333333) backgroundColor:[UIColor clearColor] font:18];
    self.visitorLable = visitorLable;
    [self addSubview:visitorLable];
    
    UILabel *timeLable = [UILabel lableWithMark:@"2017-02-02" titleColor:RGBColor(0X333333) backgroundColor:[UIColor clearColor] font:12];
    self.timeLable = timeLable;
    [self addSubview:timeLable];
    
    UIImageView *IDImageView = [UIImageView imageView:@"enter_icon_id_sel"];
    self.IDImageView = IDImageView;
    [self addSubview:IDImageView];
    
    UIImageView *photoImageView = [UIImageView imageView:@"enter_icon_id_sel"];
    self.photoImageView = photoImageView;
    [self addSubview:photoImageView];
    
    UILabel *stateLable = [UILabel lableWithMark:@" 处理 " titleColor:RGBColor(0X333333) backgroundColor:RGBColor(0X999999) font:14];
    stateLable.layer.cornerRadius = 2;
    stateLable.layer.masksToBounds = YES;
    self.stateLable = stateLable;
    [self addSubview:stateLable];
    
    UILabel *bgLable = [UILabel lableWithBackGround];
    bgLable.backgroundColor = RGBColor(0XF0F0F0);
    [self addSubview:bgLable];
    
    //2.布局子控件
    NSDictionary *dic =@{
                         @"visitorLable":visitorLable,
                         @"timeLable":timeLable,
                         @"IDImageView":IDImageView,
                         @"photoImageView":photoImageView,
                         @"stateLable":stateLable,
                         @"bgLable":bgLable
                         };
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[visitorLable]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-18-[visitorLable]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[visitorLable]-7-[timeLable]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[timeLable]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[stateLable]-12-|" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-18-[stateLable]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[IDImageView(110)]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[visitorLable]-16-[IDImageView(130)]" options:0 metrics:nil views:dic]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[IDImageView]-15-[photoImageView(110)]" options:NSLayoutFormatAlignAllTop metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[photoImageView(130)]" options:NSLayoutFormatAlignAllTop metrics:nil views:dic]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[bgLable]-0-|" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[IDImageView]-10-[bgLable(10)]" options:0 metrics:nil views:dic]];
}

- (CGFloat)headViewHeight {
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.IDImageView.frame) + 20;
}


- (void)setModel:(GYVisitor *)model {
    _model = model;
    //在这里赋值
    _IDImageView.image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.idImage];
    _photoImageView.image = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:model.faceimagebase];
    _visitorLable.text = model.name;
    _timeLable.text = [[model.visitingTime componentsSeparatedByString:@"."] firstObject];
    _stateLable.text = model.state;
    _stateLable.backgroundColor = model.backgroundColor;
}
@end
